<?php
namespace Admin\Controller;
use Think\Controller;

class NewsController extends HomeController {

	/*
	* Function:		NewsList
	* Description:	新闻列表
	*/
    public function NewsList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('News')->getNewsList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
			foreach($list as &$val){
				$info = M('news_type')->where(['id'=>$val['tid']])->find();
				$val['cate_name'] = $info['name'];
			}
		}
		$count =  D('News')->getNewsCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$cateList = M('news_type')->where(['status'=>1])->select();
		$this->assign('cateList',$cateList);
		$this->display();
    }
	
	/*
	* Function:		addNews
	* Description:	添加新闻
	*/
	public function addNews(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('News')->addNews($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
		    $this->assign('addtime',date('Y-m-d H:i:s',time()));
			$newsCateList = D('NewsType')->getNewsTypeAllList();
			$this->assign('newsCateList',$newsCateList);
			$this->display();
		}
	}
	
	/*
	* Function:		editNews
	* Description:	编辑新闻
	* @param:		$id int id
	*/
	public function editNews($id = ''){
		if(IS_POST){
			$data = I('post.');
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$ret = D('News')->updateNews($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('News')->getNewsInfo($id);
				$this->assign('info',$info);
				$newsCateList = D('NewsType')->getNewsTypeAllList();
				$this->assign('newsCateList',$newsCateList);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteNews
	* Description:	删除新闻
	* @param:		$id
	*/
	public function deleteNews($id = ''){
		if(M('news')->where(['id'=>$id])->delete()){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}

	/*
	* Function:		newsCateList
	* Description:	新闻分类列表
	*/
    public function newsCateList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('NewsType')->getNewsTypeList($page,$filter,$pageSize);
		$count =  D('NewsType')->getNewsTypeCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addGoodsCate
	* Description:	添加新闻分类
	*/
	public function addNewsCate(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			if(D('NewsType')->addNewsType($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editNewsCate
	* Description:	编辑新闻分类
	* @param:		$id int id
	*/
	public function editNewsCate($id = ''){
		if(IS_POST){
			$data = I('post.');
			$ret = D('NewsType')->updateNewsType($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('NewsType')->getNewsTypeInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
}